;
(function (call) {
    var main = {
        userInfo: null,
        mapId: "jcybMap",
        productCfg: {
            'fwgf': '基层气象灾害预警服务规范', 'fyfw': '气象灾害分级防御服务规范', 'jyzd': '重大气象灾害"叫应"制度', 'glgf': '气象信息员管理规范', 'glzd': '气象灾害预警服务留痕管理制度',
            'jkkh': '基层气象防灾减灾的监控和考核制度', 'other': '其他资料'
        },
        init: function () {
            this.bindEvt();
            this.initView();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
			if (this.userInfo.roleIds.indexOf('e9b0eaab-f2a3-4f71-9e64-644b255a706c') > -1) {
				$(".product-upload").hide();
			}
            this.getAreaData();
            $("#modal_ruler").find("#modal-title span").first().click();
        },
        // 获取区域数据
        getAreaData: function () {
            var that = this;
            $.getJSON(api.internal.business.getAreaData, function (res) {
                if (res.isSuccess) {
                    var data = res.data;
                    that.dataDisplay(data);
                    that.dataUpload(data)
					
					if (that.userInfo.roleIds.indexOf('e9b0eaab-f2a3-4f71-9e64-644b255a706c') > -1) {
						$(".xml-down i").hide();
					}
                }
            });
        },
        // 数据展示页面
        dataDisplay: function (data) {
            var $excel = $('.excel-down');
            var $xml = $('.xml-down');
            var code = Number(this.userInfo.userAreaCode);
            for (var i = 0; i < data.length; i++) {
                var areaObj = data[i];
                if (areaObj.areaName != '市区' || areaObj.areaName != '市台') {
                    var $li = $('<li><span>' + areaObj.areaName + '</span></li>');
                    if (code === areaObj.areaId)
                        $li.find('span').addClass('active');
                    $li.data('area', areaObj);
                    $excel.append($li);
                }
            }
            for (var key in this.productCfg) {
                var productNm = this.productCfg[key];
                var $li = $('<li><span>' + productNm + '</span><i>下载</i></li>');
                $li.data('productId', key);
                $xml.append($li);
            }
        },
        // 展示数据上传页面
        dataUpload: function (data) {
            var that = this;
            var $company = $('#company');
            var $category = $('#category');
            $(".upload-people .people").text(that.userInfo.smuser.name);
            for (var i = 0; i < data.length; i++) {
                var areaObj = data[i];
                var code = Number(that.userInfo.userAreaCode);
                if (code === areaObj.areaId) {
                    $company.append(`<option selected value=${areaObj.areaId}>${areaObj.areaName}</option>`);
                } else {
                    $company.append(`<option value=${areaObj.areaId}>${areaObj.areaName}</option>`);
                }
                if (code === 12) {
                    $company.removeAttr('disabled');
                } else {
                    $company.attr('disabled', 'disabled');
                }
            }
            for (var key in this.productCfg) {
                var productNm = this.productCfg[key];
                $category.append(`<option value=${key}>${productNm}</option>`);
            }
        },
        // 上传
        upload: function () {
            var areaCode = $("#company").val();
            var name = $("#excel-file").val();
            var hzm = name.substr(name.lastIndexOf(".") + 1);
            var productId = $('#category').val();
            if (name == '') {
                alert('请选择需要上传的文件');
                return
            }
            if (hzm != "doc" && hzm != 'docx') {
                alert('请选择doc格式的文件');
                return;
            }
            var forData = new FormData();
            forData.append('file', $('#excel-file')[0].files[0]);
            loading.add('#modal_ruler');
            $.ajax({
                url: api.internal.business.productUpload + areaCode + '/' + productId,
                type: "POST",
                data: forData,
                dataType: 'json',
                cache: false,
                processData: false,
                contentType: false,
                success: function (res) {
                    loading.remove();
                    if (res.isSuccess) {
                        alert("上传成功");
                        $("#file-text").text('').removeAttr('title');
                        $("#excel-file").val('')
                    } else {
                        alert("上传失败");
                    }
                }
            })
        },
        // 预览
        productPreview: function (areaCode, productId) {
            $.getJSON(api.internal.business.productPreview + areaCode + '/' + productId, function (res) {
                if (res.isSuccess) {
                    var url = res.data;
                    $("#modal_excel_Preview .modal-body").empty().append(`<iframe id="contentFrame" name="contentFrame" frameborder="0" scrolling="no" src=""></iframe>`)
                    $("#contentFrame").attr("src", "/ioffice/officeView.do?path=" + encodeURI(encodeURI(url)));
                } else {
					alert("没有可预览的产品");
					CafeJS.ModalBoxClose('excel_Preview');
				}
            })
        },
        // 预览弹窗
        preview: function (areaCode, productId) {
            var that = this;
            if ($("#modal_excel_Preview .modal-content").is(':visible')) return;
            if ($("#modal_excel_Preview").is(':hidden')) {
                $("#modal_excel_Preview").show();
                return;
            }
            CafeJS.ModalBox({
                id: 'excel_Preview',
                title: "产品展示",
                width: 1250,
                height: 750,
                m_top: 70,
                m_left: 335,
                hide: false,
                createSucc: function () {
                    that.productPreview(areaCode, productId);
                }
            });
        },
        bindEvt: function () {
            var that = this;
            $("#modal_ruler").find(".modal-title>span").click(function () {
                $(this).addClass("active").siblings().removeClass("active");
                $(".ruler-content>li").eq($(this).index()).show().siblings().hide();
            });
            $("#modal_ruler .close").click(function () {
                $("#modal_ruler").remove();
                $("#ruler").removeClass("active");
            });
            $(".excel-down").on('click', 'li span', function () {
                $(this).parent().siblings().find('span.active').removeClass("active");
                $(this).addClass("active");
            })
            $(".xml-down").on('click', 'li span', function () {
                var areaCode = $(".excel-down span.active").parent().data('area').areaId;
                var productId = $(this).parent().data('productId');
                if (!areaCode || !productId) return;
                that.preview(areaCode, productId);
            }).on('click', 'li i', function () {
                var areaCode = $(".excel-down span.active").parent().data('area').areaId;
                var productId = $(this).parent().data('productId');
                var url = api.internal.business.downProduct + areaCode + '/' + productId;
                $('#xmlIframe').attr('src', url);
            })
            $("#excel-file").change(function () {
                var val = $("#excel-file").val()
                $("#file-text").text(val).attr('title', val)
            })
            $(".upload-btn").click(function () {
                that.upload();
            })
            $(".excel-close-btn").click(function () {
                $(".excel-display").hide();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
});